package com.fatloss.angel;

import java.util.Calendar;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.format.Time;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class main extends Activity {
	
	private TextView tv;
	private Button button_ND;
	private Button button_PD;
	private Button button_ADD;
	private DBclass dbHelper;
	private ListView lv;
	private two_adapter ada2;
	private Time time_obj;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        tv = (TextView) findViewById(R.id.ttview);
        time_obj = new Time();
        time_obj.setToNow();
        dbHelper = new DBclass(this);   
        
        updateDateonText(tv);
 
        
        button_ND =  (Button)findViewById(R.id.button_ND);
        button_ND.setOnClickListener(new View.OnClickListener() {	
			public void onClick(View v) {
				// TODO Auto-generated method stub
				time_obj.monthDay+=1;
				time_obj.normalize(false);
				updateDateonText(tv);
				readFromDB();
				lv.setAdapter(ada2);
			}
		});
        
        ada2 = new two_adapter(this);
        
        readFromDB();
        
        lv= (ListView)findViewById(R.id.listView1);
        
        //lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, FOODS));
        lv.setAdapter(ada2);
        lv.setTextFilterEnabled(true);
        
        
        button_PD =  (Button)findViewById(R.id.button_PD);
        button_PD.setOnClickListener(new View.OnClickListener() {	
			public void onClick(View v) {
				// TODO Auto-generated method stub
				time_obj.monthDay-=1;
				time_obj.normalize(false);
				updateDateonText(tv);
				readFromDB();
				lv.setAdapter(ada2);
			}
		});
        
        button_ADD =  (Button)findViewById(R.id.button_addp);
        button_ADD.setOnClickListener(new View.OnClickListener() {	
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//dbHelper.insertOne(rightnow.get(Calendar.DATE));
				Intent intent = new Intent(main.this, add.class);
				Bundle bundle = new Bundle();
				bundle.putString("timestring", time_obj.format2445());
				intent.putExtras(bundle);
				startActivity(intent);
			}
		});
        
        
    }
    
    @Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		readFromDB();
		lv.setAdapter(ada2);
	}

    private void readFromDB()
    {
    	SQLiteDatabase db = dbHelper.getReadableDatabase();
        ada2.removeAll();
        
        Cursor cs = db.query(DBclass.TABLE_NAME_CALORIE, null, "time LIKE ?", new String[]{time_obj.format("%Y%m%d")+"T%"}, null, null, null);
        
        try{
        	if(cs.moveToFirst())
        	{
        		do{
        			String str=cs.getString(cs.getColumnIndex("food"));
        			String tm=cs.getString(cs.getColumnIndex("time"));
        			int calo = cs.getInt(cs.getColumnIndex("calories"));
        			int id = cs.getInt(cs.getColumnIndex("_id"));
        			ada2.add(str, tm, calo, id);
        		}while(cs.moveToNext());
        	}
        } finally{
        	cs.close();
        }

        db.close();
    }
    
	private void updateDateonText(TextView tv)
    {
    	tv.setText(time_obj.format("%Y-%m-%d"));
    }

}